/***************************************************
 ** @Desc : This file for 请求方法
 ** @Time : 2019.01.09 16:36 
 ** @Author : Joker
 ** @File : request_method
 ** @Last Modified by : Joker
 ** @Last Modified time: 2019.01.09 16:36
 ** @Software: GoLand
****************************************************/
package utils

import (
	"net/http"
	"io/ioutil"
	"strings"
)

type RequestMethod struct{}

func (*RequestMethod) HttpGetRequest(url_data string) (string, error) {
	resp, err := http.Get(url_data)
	if err != nil {
		return "Failed to send get-request:", err
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "Get-Response`s content is error:", err
	}
	defer resp.Body.Close()

	return string(body), nil
}


//发送get请求
func (*RequestMethod) XinLangHttpGetRequest(url_data, cookie string) (string, error) {
	client := http.Client{}
	req, err := http.NewRequest("GET", url_data, nil)
	if err != nil {
		return "Failed to structure get-request to XinLang:", err
	}

	req.Header.Add("X-Requested-With", "XMLHttpRequest")
	req.Header.Add("Content-Type", `application/x-www-form-urlencoded`)
	req.Header.Add("User-Agent", `Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36`)
	req.Header.Add("Referer", "https://weibo.com/signup/mobile.php?lang=zh-cn&inviteCode=&from=&appsrc=&backurl=&showlogo=")
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Cookie", cookie)

	resp, err := client.Do(req)
	if err != nil {
		return "Failed to send get-request to XinLang:", err
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "XinLang get-response`s content is error:", err
	}
	defer resp.Body.Close()

	return string(body), nil
}

func (*RequestMethod) HuYaHttpPostRequest(addr, url_data string) (string, error) {
	client := http.Client{}
	req, err := http.NewRequest("POST", addr, strings.NewReader(url_data))
	if err != nil {
		return "Failed to structure post-request to HuYa:", err
	}

	req.Header.Add("Content-Type", "application/json")

	resp, err := client.Do(req)
	if err != nil {
		return "Failed to send post-request to HuYa:", err
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "HuYa post-response`s content is error:", err
	}
	defer resp.Body.Close()

	return string(body), nil
}